package org.example.spring.controller;

import org.example.spring.entity.Books;
import org.example.spring.mapper.BookMapper;
import org.example.spring.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

//@CrossOrigin(origins = "http://192.168.0.101:8080/")
//@CrossOrigin(origins = "http://172.20.10.4:8080/")
@RestController
@RequestMapping("/books")
public class BookController {
    @Autowired
    private BookMapper mapper;
    @Autowired
    private BookService service;
    @Autowired
    private BookMapper bookMapper;

    //    查询
    @GetMapping("show")
    public List<Books> getAllBooks() {
        return mapper.getAllBooks();

    }

    //    删除
    @DeleteMapping("delete/{id}")
    public Integer deleteUser(@PathVariable Integer id) {
        return mapper.deleteBooks(id);
    }

    //    更新
    @PostMapping("/save")
    public Integer saveUser(@RequestBody Books books) {
        return service.save(books);
    }

    //    分页查询
    @GetMapping("/page")
    public Map<String,Object> page(@RequestParam int page, @RequestParam int size) {
        page=(page-1)*size;
        List<Books> data=bookMapper.selectPage(page,size);
        Integer total=bookMapper.selectTotal();
        Map<String,Object> res=new HashMap();
        res.put("data",data);
        res.put("total",total);
        return res;
    }
}